package com.bigkoo.pickerviewdemo.dayweekhalfhour;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
import com.bigkoo.pickerview.lib.WheelView;
import com.bigkoo.pickerview.view.BasePickerView;
import com.bigkoo.pickerviewdemo.R;
import com.bigkoo.pickerviewdemo.util.DateUtil;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * 创建时间：2016年08月11日14:54 <br>
 * 作者：fuchaoyang <br>
 * 描述：
 */
public class DayWeekHalfHourTimePicker extends BasePickerView implements View.OnClickListener {

  private static final String TAG = DayWeekHalfHourTimePicker.class.getSimpleName();
  private View btnSubmit, btnCancel;
  private static final String TAG_SUBMIT = "submit";
  private static final String TAG_CANCEL = "cancel";
  private OnTimeSelectListener timeSelectListener;
  private int mStartTime, mEndTime, mCurrentHour;
  private boolean mStartHalf, mEndHalf;
  private String mStartDate, mEndDate;
  private HalfHourNumericWheelAdapter mHourAdapter;
  private ArrayWheelAdapter<DayWeekHalfHourTimeInfo> mDayAdapter;
  private WheelView mDay, mHours;

  public DayWeekHalfHourTimePicker(Context context){
    super(context);
    LayoutInflater.from(context).inflate(R.layout.view_report_timepicker, contentContainer);
    // -----确定和取消按钮
    btnSubmit = findViewById(R.id.btnSubmit);
    btnSubmit.setTag(TAG_SUBMIT);
    btnCancel = findViewById(R.id.btnCancel);
    btnCancel.setTag(TAG_CANCEL);
    btnSubmit.setOnClickListener(this);
    btnCancel.setOnClickListener(this);

    // 日期和周几
    mDay = (WheelView) findViewById(R.id.day);
    mDay.setCyclic(false);
    //mDayAdapter = new DayArrayAdapter(getData(0,1472659200L));
    mDayAdapter = new DayArrayAdapter(getData(0,1493568000L));
    mDay.setAdapter(mDayAdapter);
  }

  public DayWeekHalfHourTimePicker(Context context, String startDate, String endDate, int startTime,
      int endTime, boolean startHalf, boolean endHalf, int currentHour) throws ParseException {
    super(context);
    mStartTime = startTime;
    mEndTime = endTime;
    mStartDate = startDate;
    // mStartDate = "2015-12-28";
    // mEndDate = "2016-2-5";
    mEndDate = endDate;

    mStartHalf = startHalf;
    mEndHalf = endHalf;
    mCurrentHour = currentHour;
    LayoutInflater.from(context).inflate(R.layout.view_report_timepicker, contentContainer);
    // -----确定和取消按钮
    btnSubmit = findViewById(R.id.btnSubmit);
    btnSubmit.setTag(TAG_SUBMIT);
    btnCancel = findViewById(R.id.btnCancel);
    btnCancel.setTag(TAG_CANCEL);
    btnSubmit.setOnClickListener(this);
    btnCancel.setOnClickListener(this);

    // 日期和周几
    mDay = (WheelView) findViewById(R.id.day);
    mDayAdapter = new DayArrayAdapter(getData(0,1472659200L));
    mDay.setAdapter(mDayAdapter);

    //mHours = (WheelView) findViewById(R.id.hour);
    //if (mCurrentHour == 0) {
    //  mHourAdapter = new HalfHourNumericWheelAdapter(mStartTime, mStartHalf, mEndTime, mEndHalf);
    //} else {
    //  mHourAdapter = new HalfHourNumericWheelAdapter(mCurrentHour, mStartHalf, mEndTime, mEndHalf);
    //}
    //
    //mHours.setAdapter(mHourAdapter);
  }
  public static SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyy年MM月dd日  E",Locale.CHINA);

  private ArrayList<DayWeekHalfHourTimeInfo> getData(long startData, long endData) {
    ArrayList<DayWeekHalfHourTimeInfo> list = new ArrayList<>();
    int offset = DateUtil.getDayOffset(System.currentTimeMillis(), endData * 1000);
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());

    for (int i = 0; i < offset; i++) {
      calendar.add(Calendar.DATE, 1);
      // 展示：08月13日
      String format = mDateFormat.format(calendar.getTime());
      DayWeekHalfHourTimeInfo timeInfo = new DayWeekHalfHourTimeInfo(format, "");
      list.add(timeInfo);
    }
    return list;
  }

  @Override public void onClick(View v) {

  }

  private class DayArrayAdapter extends ArrayWheelAdapter<DayWeekHalfHourTimeInfo> {

    public DayArrayAdapter(ArrayList<DayWeekHalfHourTimeInfo> items) {
      super(items, 5);
    }
  }

  public interface OnTimeSelectListener {

    public void onTimeSelect(String date, long dateL);
  }

  public void setOnTimeSelectListener(OnTimeSelectListener timeSelectListener) {
    this.timeSelectListener = timeSelectListener;
  }
}
